package com.example.service;

import com.example.domain.Student;
import com.example.domain.Teacher;
import com.example.repository.StudentRepository;
import com.example.repository.TeacherRepository;
import com.example.util.EasyuiResult;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Service
@Transactional
public class TeacherService {
    @Autowired
    private TeacherRepository teacherRepository;
    
    // public Teacher getStudent(Teacher user) {
    //     final Optional<Teacher> optionalUser = teacherRepository.findByAccountAndPassword(user.getAccount(),
    //             user.getPassword());
    //     return optionalUser.orElseGet(Teacher::new);
    // }
    
    
    public EasyuiResult<Teacher> list(final int type, final int page, final int rows, final String sortColumn, final String order) {
        Sort sort = new Sort(Sort.Direction.fromString(order), sortColumn);
        final PageRequest pageRequest = PageRequest.of(page, rows, sort);
        final Page<Teacher> all = teacherRepository.findAllByType(type, pageRequest);
        EasyuiResult<Teacher> result = new EasyuiResult(all.getTotalElements(),all.getContent()) ;
        return result;
    }
    
    public List<Teacher> getAllTeachers(){
        final Iterable<Teacher> all = teacherRepository.findAll();
        return Lists.newArrayList(all);
    }
    
    public List<Teacher> all() {
        final Iterable<Teacher> all = teacherRepository.findAll();
        return Lists.newArrayList(all);
    
    }
    
    public Teacher getById(final int teacherid) {
        return teacherRepository.findById(teacherid).orElseGet(Teacher::new);
    }
}
